
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Graphics;

public final class CSprite
{
    /**
     * Every sprite file is a CSprite instance
     */
    public CSprite()
    {
    }
    /**
     * Image data RGB
     */
    private int[] m_RGBDatas = null;
    private int m_RGBDatasLength = -1;
    /**
     * the info (x,y,w,h) of modules, continuously
     */
    private int[] m_moduleInfos = null;
    private int m_moduleLength = -1;
    /**
     * the scan length of the image, normal is image.getWidth()
     */
    private int m_imageWidth = 0;
    private int m_imageHeight = 0;
    private int m_scanLength = 0;

    /**
     * Load data file RGB
     */
    public void loadData(String dataPath)
    {
        InputStream inputStream = getClass().getResourceAsStream(dataPath);
        //InputStream inputStream = getClass().getResourceAsStream("E:/Projects/EasyRGB/res.easyrgb"); do not work with directed directory path
        if (inputStream == null)
        {
            System.out.println("ERROR load file!!!!!!!!!!");
            return;
        }
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        int countRGB = 0, countModules = 0;
        while (true)
        {
            try
            {
                if (m_RGBDatasLength == -1)//init the RGB data
                {
                    m_imageWidth = dataInputStream.readInt();
                    m_imageHeight = dataInputStream.readInt();
                    m_scanLength = dataInputStream.readInt();
                    m_RGBDatasLength = m_imageWidth * m_imageHeight;
                    m_RGBDatas = new int[m_RGBDatasLength];
                }
                else if (countRGB < m_RGBDatasLength)//read all the RGB data
                {
                    m_RGBDatas[countRGB++] = dataInputStream.readInt();
                }
                else if (m_moduleLength == -1)//init the info array of modules
                {
                    m_moduleLength = dataInputStream.readInt();
                    m_moduleInfos = new int[4 * m_moduleLength];
                }
                else//read all infos of modules
                {
                    m_moduleInfos[countModules++] = dataInputStream.readInt();
                }
            } catch (EOFException eOFException)//end of file will stop
            {
                break;
            } catch (IOException iOException)
            {
                System.out.println("ERROR get data!!!!!!!!!!");
                iOException.printStackTrace();
            }
        }
    }

    /**
     * paint module
     */
    public void paintModule(Graphics g, int moduleIndex, int x, int y)
    {
        //get the info of the module        
        int realIndex = moduleIndex << 2,
                moduleX = m_moduleInfos[realIndex],
                moduleY = m_moduleInfos[realIndex + 1],
                moduleW = m_moduleInfos[realIndex + 2],
                moduleH = m_moduleInfos[realIndex + 3];
        int offsetImage = m_scanLength * moduleY + moduleX;
        //paint the module
        g.drawRGB(m_RGBDatas, offsetImage, m_scanLength, x, y, moduleW, moduleH, false);
    }

    /**
     * paint module and process alpha
     */
    public void paintModule(Graphics g, int moduleIndex, int x, int y, boolean isProcessAlpha)
    {
        //get the info of the module        
        int realIndex = moduleIndex << 2,
                moduleX = m_moduleInfos[realIndex],
                moduleY = m_moduleInfos[realIndex + 1],
                moduleW = m_moduleInfos[realIndex + 2],
                moduleH = m_moduleInfos[realIndex + 3];
        int offsetImage = m_scanLength * moduleY + moduleX;
        //paint the module
        g.drawRGB(m_RGBDatas, offsetImage, m_scanLength, x, y, moduleW, moduleH, isProcessAlpha);
    }
}

